Page({
  data: {
   user:wx.getStorageSync('user')
  },

  // 修改头像
  async changeAvatar() {
 
  const res =  await getApp().$upload('avatarUrl')
  this.setData({
    'user.avatar_url':res.data,
    avatarUrl:res.data
  })
  console.log(res)
  },

  // 昵称修改
  onNicknameChange(e) {
    this.setData({
      nickname: e.detail.value,
    });
  },

  // 保存个人信息
 async  saveProfile() {
   const res = await getApp().$request('/api/updateUserInfo','POST',{nickname:this.data.nickname,avatar_url:this.data.avatarUrl,id:this.data.user.id})
    // 可以在这里实现将用户信息保存到后台服务
    wx.showToast({
      title: '保存成功',
      icon: 'success',
    });
this.getUserInfo()
    // 模拟保存后的操作
    console.log('用户头像:', this.data.avatarUrl);
    console.log('用户昵称:', this.data.nickname);
  },
 async getUserInfo(){
const res = await getApp().$request('/api/getUserInfo','POST',{id:this.data.user.id})
this.setData({
  user:res.data
})
wx.setStorageSync('user', res.data)
wx.switchTab({
  url: '/pages/profile/profile',
})
  },
  onLoad() {
    // 模拟加载用户信息
    this.setData({
      avatarUrl: wx.getStorageSync('user').avatar_url,
      nickname: wx.getStorageSync('user').nickname,
    });
  },
});
